package Service.Impl;

import Dao.UserMapper;
import Service.UserService;
import Utils.Md5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /*
             用户分页 + 多条件组合查询
      */
    @Override
    public PageInfo findAllUserByPage(UserVO userVO) {

        PageHelper.startPage(userVO.getCurrentPage(),userVO.getPageSize());
        List<User> page = userMapper.findAllUserByPage(userVO);

        PageInfo<User> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    /*
         用户登录 （根据用户名 查询具体的用户信息）
    */
    @Override
    public User login(User user) throws Exception {

        //1. 调用Mapper 方法
        // login包含了 密文密码
        User login = userMapper.login(user);

        if(login != null && Md5.verify(user.getPassword(),"lagou",login.getPassword())){
            return login;
        }else{
            return null;
        }

    }

    /*
        根据用户id 查询用户拥有的角色信息
    */
    @Override
    public List<Role> findUserRelationRoleById(int userid) {
        List<Role> roleList = userMapper.findUserRelationRoleById(userid);
        return roleList;
    }

    /*
       更改用户角色
      根据用户清空关联中间表 + 重新添加中间关联表
    */

    @Override
    public void userContextRole(UserVO userVO) {

        //1. 根据用户Id 清空关联表关系
        userMapper.deleteUserRelationRoleById(userVO.getUserId());

        //2. 重新添加关联表关系
        for (Integer roleId : userVO.getRoleIdList()) {

            // 封装数据　
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVO.getUserId());
            user_role_relation.setRoleId(roleId);
            user_role_relation.setCreatedBy("mmj");
            user_role_relation.setUpdatedby("mmj");

            Date date = new Date();
            user_role_relation.setCreatedTime(date);
            user_role_relation.setUpdatedTime(date);

            userMapper.addUserRelationRoleById(user_role_relation);

        }

    }

    /*
        获取用户权限，进行菜单动态展示
    */
    @Override
    public ResponseResult getUserPermissions(int userid) {

        // 1. 根据当前用户id 查询该用户拥有的角色
        List<Role> roles = userMapper.findUserRelationRoleById(userid);

        //2.根据角色Id(roleId) 查询到父菜单
        ArrayList<Integer> list = new ArrayList<>();
        for (Role role : roles) {
            list.add(role.getId());
        }
        List<Menu> menu = userMapper.findMenuByRoleId(list);

        //3.对父菜单关联的子菜单进行关联查询
        ArrayList<Integer> father = new ArrayList<>();
        for (Menu menu1 : menu) {
            List<Menu> son = userMapper.findSubMenuByPid(menu1.getId());
            menu1.setSubmenulist(son);
        }

        //4. 根据角色Id(roleId) 获取到资源信息
        List<Resource> resourceList = userMapper.findResourceByRoleId(list);

        //5.封装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("menuList",menu);
        map.put("resourcesList",resourceList);
        return new ResponseResult(true,1,"权限信息回显成功",map);
    }
}
